package com.songtech.uitest.cases;

import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.LogInAndOutHandler;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.resources.ServerResource;
import com.songtech.uitest.util.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Iterator;

/**
 * 一个单一个流程，对应一个测试类。
 * 若有多个单多个流程要跑，就另开一个测试类单独对应一个数据源（如单独一张excel）
 * 不要多个单多个流程写在一个测试类里，初始化和重试时容易出问题
 */
@ContextConfiguration(locations = {"classpath:spring-test-config.xml"})
public class TestCustomFlow1 extends AbstractTestNGSpringContextTests {

    @Autowired
    private RollbackHelper rollbackHelper;

    @Autowired
    private IndentResource indentResource;

    @Autowired
    private Indent indent;

    @Autowired
    private ConfigLoader configLoader;

    @Autowired
    SumUpHelper sumUpHelper;

    private Logger logger = LoggerFactory.getLogger(TestCustomFlow1.class);

    private final static String xlsFileName = "铁塔复杂全流程.xls";


    @BeforeClass
    public void init() throws Exception {
        //加载外部配置文件（若不加载，默认由spring加载application.properties配置文件）
        configLoader.loadFromFile(xlsFileName);
        indent.setMobileId(indentResource.getMobileID());
        indent.setTowerId(indentResource.getTowerID());
		indent.setFlow(indentResource.getCustomFlow());
		indent.setStepPosition(0);
        rollbackHelper.rollbackCurrentDriverToInit();
    }



    //走自定义流程，里边不要加其他代码，因为在retry时此方法会重新重头执行
    @Test(enabled=true)
    public void testCustomFlow() throws Exception{
        rollbackHelper.run(indent.getFlow());

    }

    //如果要在此测试类里添加多个流程，可以这样写：(不推荐，建议另写一个测试类)
//    @Test(enabled=true)
//    public void testAnotherFlow() throws Exception{
//        indent.setMobileId("...");
//        indent.setTowerId("...");
//        indent.setFlow(indentResource.getWholeFlow());
//        indent.setStepPosition(rollbackHelper.getCounter());
//
//        rollbackHelper.run(indent.getFlow());
//
//    }

    @AfterClass
    public void tearDown() {
        logger.info(xlsFileName + "配置流程运行完毕。");
        logger.info("单子当前值：" + indent.getMobileId() +","+ indent.getTowerId() +","+ indent.getCurrentBuildType() +",流程下标位置："+ indent.getStepPosition());

        sumUpHelper.printResult();
        sumUpHelper.clearResult();
    }

}
